/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controllers;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import java.util.Vector;

import Models.NhatKyHT_Model;
import Models.KhuVuc_Model;
import Models.ThoiDiem_Model;
import Models.PhiPV_Model;
import Beans.NhanVien;

/**
 *
 * @author Venus
 */
public class PhiPV_Controller extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            if (session.getAttribute("langid") == null) {
                response.sendRedirect("index.jsp");
                return;
            }
            session.removeAttribute("DSMA");
            session.removeAttribute("DSNMA");
            session.removeAttribute("item");
            String action = request.getParameter("action");
            int langid = (Integer) session.getAttribute("langid");
            if (action.equals("Show")) { 
                String item = request.getParameter("item");
                PhiPV_Model ppvm = new PhiPV_Model();
                Vector vDSPPV = new Vector();
                vDSPPV = ppvm.LayDSPPV();
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector vDSKV = new Vector();
                vDSKV = kvm.LayDSKVPPV(langid);
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector vDSTD = new Vector();
                vDSTD = tdm.LayDSTDPPV(langid);
                request.setAttribute("DSPPV", vDSPPV);
                request.setAttribute("DSKV", vDSKV);
                request.setAttribute("DSTD", vDSTD);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/newsercharge.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("CreateSerCharge")) {
                String makv = request.getParameter("makv");
                String matd = request.getParameter("matd");
                String servicecharge = request.getParameter("servicecharge");
                PhiPV_Model.TaoPhiPV(makv, matd, servicecharge);
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(2, nv.getMaNV(), "servicecharge");
                out.print("success");
            }
            else if (action.equals("ShowDSPPV")) {
                String item = request.getParameter("item");
                PhiPV_Model ppvm = new PhiPV_Model();
                Vector vDSPPV = new Vector();
                vDSPPV = ppvm.LayDSPPV();
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector vDSKV = new Vector();
                vDSKV = kvm.LayDSKVPPV(langid);
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector vDSTD = new Vector();
                vDSTD = tdm.LayDSTDPPV(langid);
                request.setAttribute("DSPPV", vDSPPV);
                request.setAttribute("DSKV", vDSKV);
                request.setAttribute("DSTD", vDSTD);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/listsercharge.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortTenKVPPV")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                PhiPV_Model ppvm = new PhiPV_Model();
                Vector vDSPPV = new Vector();
                vDSPPV = ppvm.SortTenKVPPV(langid, order);
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector vDSKV = new Vector();
                vDSKV = kvm.LayDSKVPPV(langid);
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector vDSTD = new Vector();
                vDSTD = tdm.LayDSTDPPV(langid);
                request.setAttribute("DSPPV", vDSPPV);
                request.setAttribute("DSKV", vDSKV);
                request.setAttribute("DSTD", vDSTD);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsercharge.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortTenTDPPV")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                PhiPV_Model ppvm = new PhiPV_Model();
                Vector vDSPPV = new Vector();
                vDSPPV = ppvm.SortTenTDPPV(langid, order);
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector vDSKV = new Vector();
                vDSKV = kvm.LayDSKVPPV(langid);
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector vDSTD = new Vector();
                vDSTD = tdm.LayDSTDPPV(langid);
                request.setAttribute("DSPPV", vDSPPV);
                request.setAttribute("DSKV", vDSKV);
                request.setAttribute("DSTD", vDSTD);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsercharge.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortPPV")) {
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                PhiPV_Model ppvm = new PhiPV_Model();
                Vector vDSPPV = new Vector();
                vDSPPV = ppvm.SortPPV(langid, order);
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector vDSKV = new Vector();
                vDSKV = kvm.LayDSKVPPV(langid);
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector vDSTD = new Vector();
                vDSTD = tdm.LayDSTDPPV(langid);
                request.setAttribute("DSPPV", vDSPPV);
                request.setAttribute("DSKV", vDSKV);
                request.setAttribute("DSTD", vDSTD);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsercharge.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SearchSerCharge")) {
                String tenkv = request.getParameter("tenkv");
                String tentd = request.getParameter("tentd");
                String ppv = request.getParameter("ppv");
                String item = request.getParameter("item");
                PhiPV_Model ppvm = new PhiPV_Model();
                Vector vDSPPV = new Vector();
                vDSPPV = ppvm.SearchSerCharge(tenkv, tentd, ppv, langid);
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector vDSKV = new Vector();
                vDSKV = kvm.LayDSKVPPV(langid);
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector vDSTD = new Vector();
                vDSTD = tdm.LayDSTDPPV(langid);
                request.setAttribute("DSPPV", vDSPPV);
                request.setAttribute("DSKV", vDSKV);
                request.setAttribute("DSTD", vDSTD);
                request.setAttribute("tenkv", tenkv);
                request.setAttribute("tentd", tentd);
                request.setAttribute("ppv", ppv);
                request.setAttribute("item", item);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssearchsercharge.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchTenKVPPV")) {
                String tenkv = request.getParameter("tenkv");
                String tentd = request.getParameter("tentd");
                String ppv = request.getParameter("ppv");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                PhiPV_Model ppvm = new PhiPV_Model();
                Vector vDSPPV = new Vector();
                vDSPPV = ppvm.SortSearchTenKVPPV(tenkv, tentd, ppv, langid, order);
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector vDSKV = new Vector();
                vDSKV = kvm.LayDSKVPPV(langid);
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector vDSTD = new Vector();
                vDSTD = tdm.LayDSTDPPV(langid);
                request.setAttribute("DSPPV", vDSPPV);
                request.setAttribute("DSKV", vDSKV);
                request.setAttribute("DSTD", vDSTD);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "1");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                request.setAttribute("tenkv", tenkv);
                request.setAttribute("tentd", tentd);
                request.setAttribute("ppv", ppv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchsercharge.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchTenTDPPV")) {
                String tenkv = request.getParameter("tenkv");
                String tentd = request.getParameter("tentd");
                String ppv = request.getParameter("ppv");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                PhiPV_Model ppvm = new PhiPV_Model();
                Vector vDSPPV = new Vector();
                vDSPPV = ppvm.SortSearchTenTDPPV(tenkv, tentd, ppv, langid, order);
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector vDSKV = new Vector();
                vDSKV = kvm.LayDSKVPPV(langid);
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector vDSTD = new Vector();
                vDSTD = tdm.LayDSTDPPV(langid);
                request.setAttribute("DSPPV", vDSPPV);
                request.setAttribute("DSKV", vDSKV);
                request.setAttribute("DSTD", vDSTD);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "2");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                request.setAttribute("tenkv", tenkv);
                request.setAttribute("tentd", tentd);
                request.setAttribute("ppv", ppv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchsercharge.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("SortSearchPPV")) {
                String tenkv = request.getParameter("tenkv");
                String tentd = request.getParameter("tentd");
                String ppv = request.getParameter("ppv");
                String item = request.getParameter("item");
                String order = request.getParameter("order");
                String sort = request.getParameter("sort");
                String sortkv = request.getParameter("sortkv");
                PhiPV_Model ppvm = new PhiPV_Model();
                Vector vDSPPV = new Vector();
                vDSPPV = ppvm.SortSearchPPV(tenkv, tentd, ppv, langid, order);
                KhuVuc_Model kvm = new KhuVuc_Model();
                Vector vDSKV = new Vector();
                vDSKV = kvm.LayDSKVPPV(langid);
                ThoiDiem_Model tdm = new ThoiDiem_Model();
                Vector vDSTD = new Vector();
                vDSTD = tdm.LayDSTDPPV(langid);
                request.setAttribute("DSPPV", vDSPPV);
                request.setAttribute("DSKV", vDSKV);
                request.setAttribute("DSTD", vDSTD);
                request.setAttribute("item", item);
                request.setAttribute("order", order);
                request.setAttribute("test", "3");
                request.setAttribute("sort", sort);
                request.setAttribute("sortkv", sortkv);
                request.setAttribute("tenkv", tenkv);
                request.setAttribute("tentd", tentd);
                request.setAttribute("ppv", ppv);
                RequestDispatcher rd = request.getRequestDispatcher("admin/rssortsearchsercharge.jsp");
                rd.forward(request, response);
            }
            else if (action.equals("UpdatePPV")) {
                String ppv = request.getParameter("ppv");
                String makv = request.getParameter("makv");
                String matd = request.getParameter("matd");
                if(PhiPV_Model.EditPPV(ppv, makv, matd)){
                    out.print("success");
                }
                NhanVien nv = new NhanVien();
                nv = (NhanVien) session.getAttribute("nv");
                NhatKyHT_Model.GhiNhatKy(3, nv.getMaNV(), "servicecharge");
            }
            else if (action.equals("Check")) {
                String makv = request.getParameter("makv");
                String matd = request.getParameter("matd");
                if(PhiPV_Model.Check(makv, matd)){
                    out.print("exist");
                }
                else{
                    out.print("not exist");
                }
            }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
